package cn.chenmanman.system.service.impl;

import cn.chenmanman.model.system.SysRole;
import cn.chenmanman.model.system.SysUserRole;
import cn.chenmanman.model.vo.RouterVo;
import cn.chenmanman.model.vo.SysUserQueryVo;
import cn.chenmanman.system.service.SysMenuService;
import cn.chenmanman.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.chenmanman.model.system.SysUser;
import cn.chenmanman.system.service.SysUserService;
import cn.chenmanman.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 17383
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2022-11-03 19:37:50
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> sysUserIpage, SysUserQueryVo sysUserQueryVo) {
        return baseMapper.selectPage(sysUserIpage, sysUserQueryVo);
    }

    @Override
    public void userDisable(Integer id, Integer status) {
        SysUser user = getById(id);
        user.setStatus(status);
        updateById(user);
    }

    @Override
    public SysUser getUserInfoByUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser user = getOne(queryWrapper);
        return user;
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser user = getOne(queryWrapper);
        List<RouterVo> routerVoList = sysMenuService.getUserMenuList(user.getId());
        List<String> permList = sysMenuService.getUserButtonList(user.getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roles", user.getRoleList());
        map.put("name", user.getName());
        map.put("avatar", user.getHeadUrl());
        map.put("buttons", permList);
        map.put("routers", routerVoList);
        return map;
    }

}




